package com.wdcg.view.adapter;

import java.util.List;
import java.util.zip.Inflater;

import com.wdcg.R;
import com.wdcg.R.string;
import com.wdcg.model.dao.DwarfDescription;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

public class DwarfsAdapter implements ListAdapter {

	private List<DwarfDescription> mDwarfs;
	private Context mContext;
	private OnClickListener mOnItemClickListener;
	
	public OnClickListener getOnItemClickListener() {
		return mOnItemClickListener;
	}

	public void setOnItemClickListener(OnClickListener aOnItemClickListener) {
		mOnItemClickListener = aOnItemClickListener;
	}

	public DwarfsAdapter(Context aContext, List<DwarfDescription> aDwarfs) {
		mContext = aContext;
		mDwarfs = aDwarfs;
	}
	
	public int getCount() {
		return mDwarfs.size();
	}

	public Object getItem(int aArg0) {
		return mDwarfs.get(aArg0);
	}

	public long getItemId(int aArg0) {
		return getItem(aArg0).hashCode();
	}

	public int getItemViewType(int aArg0) {
		return IGNORE_ITEM_VIEW_TYPE;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View lView;
		DwarfDescription lDescription = (DwarfDescription) getItem(position);
		if (convertView != null) {
			lView = convertView;
		} else {
			lView = LayoutInflater.from(mContext).inflate(R.layout.item_list_achievements, parent, false);
		}
		
		ImageView lImage = (ImageView) lView.findViewById(R.id.dwarfLogoImageView);
		lImage.setImageResource(
				mContext.getResources()
				.getIdentifier(
						"@drawable/"+lDescription.getDwarf().getImgTitle(),
						null,
						mContext.getPackageName()));
		
		TextView lTextView = (TextView) lView.findViewById(R.id.dwarfNameTextView);
		//lTextView.setText(lDescription.getName());
		
		Button lButton = (Button) lView.findViewById(R.id.dwarfActionButton);
		String lText;
		String lName;
		
		if (lDescription.getDwarf().getIsWaitingForPlay()) {
			lText = mContext.getString(R.string.achievements_list_button_minigame);
			lName = mContext.getString(R.string.achievements_list_unknown_dwarf_name);
		} else {
			lText = mContext.getString(R.string.achievements_list_button_info);
			lName = lDescription.getName();
		}
		lTextView.setText(lName);
		lButton.setText(lText);
		lButton.setTag(lDescription.getDwarf().getId());
		lButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View aV) {
				if (mOnItemClickListener != null) {
					mOnItemClickListener.onClick(aV);
				}
			}
		});
		
		return lView;
	}

	public int getViewTypeCount() {
		return 1;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isEmpty() {
		return mDwarfs.isEmpty();
	}

	public void registerDataSetObserver(DataSetObserver aArg0) {
		// TODO Auto-generated method stub

	}

	public void unregisterDataSetObserver(DataSetObserver aArg0) {
		// TODO Auto-generated method stub

	}

	public boolean areAllItemsEnabled() {
		return true;
	}

	public boolean isEnabled(int aArg0) {
		return true;
	}
}
